<?php

namespace Action\Flow\GenericFlow\Item;

class Add extends \Library\Action\UserAction 
	implements \Library\Action\AjaxAction {

	private $_flow;
	private $_item;

	public function execute() {
		parent::execute();
		
		$flow_id = $this->getParam( 'id' );
		$this->_flow = \Bean\Flow::get( $flow_id );
		$message = trim( $this->getParam( 'message' ) );
		$joins = (array) $this->getParam( 'joins' );

		_a( $this->_flow->isWritable(), 'This wall is not writable' );
		_a( ($message || $joins) ? true : false, 'Message musn\'t be empty' );
		
		$this->_item = $this->_flow->addItem( array( 'message' => $message ) );
		
		foreach( $joins as $join ) {
			switch( $join['join'] ) {
				case 'link':
					$this->_item->addLink( array(
						'title' => $join['title'],
						'image' => $join['image'],
						'url' => $join['url'],
						'description' => $join['description'],
						'type' => $join['type'],
						'site_name' => $join['site_name']
					) );
					break;
			}
		}
	}
	
	public function getItem() {
		return $this->_item;
	}

}
